package org.jeecg.modules.bpm.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

public class JoaUtil {
	
	/**
	 * 	查询待审批的单据
	 * @param bizTaskType 业务类型1待我审批2我发起的申请
	 * @param queryWrapper 查询器
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 */
	@SuppressWarnings("rawtypes")
	public static void filterRunningTask(String bizTaskType,QueryWrapper queryWrapper,String userName,String flowCode) {
		StringBuilder sb = new StringBuilder(" (");
		if("1".equals(bizTaskType)) {
			sb.append(" ID ").append(" in ").append("(");
			sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
			sb.append(" RIGHT JOIN (");
			sb.append(" select * from (");
			sb.append(" (select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
			sb.append(" WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' ");
			sb.append("	and ( I.USER_ID_ = {0}  or I.GROUP_ID_ IN ( select g.GROUP_ID_ from ACT_ID_MEMBERSHIP g where g.USER_ID_ = {0}  )  ) ");
			sb.append(" and RES.SUSPENSION_STATE_ = 1 ");
			sb.append(" ) union (select distinct RES.*  from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = {0} ");
			sb.append(" )) v ");
			sb.append(" ) art ");
			sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
			sb.append(" where fd.RELATION_CODE = {1}  ");
			sb.append(" )");
			sb.append(" )");
			queryWrapper.apply(sb.toString(), userName,flowCode);
		}else {
			sb.append(" CREATE_BY = {0}");
			sb.append(" )");
			queryWrapper.apply(sb.toString(), userName);
		}
		
	}
	
	
	/**
	 * 	查询待审批的单据
	 * @param bizTaskType 业务类型1待我审批2我发起的申请
	 * @param queryWrapper 查询器
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 * @param taskDefKey 任务key
	 */
	@SuppressWarnings("rawtypes")
	public static void filterRunningTask(String bizTaskType,QueryWrapper queryWrapper,String userName,String flowCode,String taskDefKey) {
		StringBuilder sb = new StringBuilder(" (");
		if("1".equals(bizTaskType)) {
			sb.append(" ID ").append(" in ").append("(");
			sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
			sb.append(" RIGHT JOIN (");
			sb.append(" select * from (");
			sb.append(" (select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
			sb.append(" WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' ");
			sb.append("	and ( I.USER_ID_ = {0}  or I.GROUP_ID_ IN ( select g.GROUP_ID_ from ACT_ID_MEMBERSHIP g where g.USER_ID_ = {0}  )  ) ");
			sb.append(" and RES.SUSPENSION_STATE_ = 1 ");
			sb.append(" ) union (select distinct RES.*  from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = {0} ");
			sb.append(" )) v ");
			sb.append(" ) art ");
			sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
			sb.append(" where fd.RELATION_CODE = {1}  ");
			sb.append(" and art.TASK_DEF_KEY_ = {2} ");
			sb.append(" )");
			sb.append(" )");
			queryWrapper.apply(sb.toString(), userName,flowCode,taskDefKey);
		}else {
			sb.append(" CREATE_BY = {0}");
			sb.append(" )");
			queryWrapper.apply(sb.toString(), userName);
		}
	}
	
	/**
	 * 	查询待审批的单据
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 * @param taskDefKey 任务key
	 */
	public static String getMyTaskSql(String userName,String flowCode) {
		StringBuilder sb = new StringBuilder(" ");
		sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
		sb.append(" RIGHT JOIN (");
		sb.append(" select * from (");
		sb.append(" (select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
		sb.append(" WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' ");
		sb.append("	and ( I.USER_ID_ = '"+userName+"'  or I.GROUP_ID_ IN ( select g.GROUP_ID_ from ACT_ID_MEMBERSHIP g where g.USER_ID_ = '"+userName+"'  )  ) ");
		sb.append(" and RES.SUSPENSION_STATE_ = 1 ");
		sb.append(" ) union (select distinct RES.*  from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = '"+userName+"' ");
		sb.append(" )) v ");
		sb.append(" ) art ");
		sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
		sb.append(" where fd.RELATION_CODE = '"+flowCode+"'  ");
	    return sb.toString();
	}
	
	/**
	 * 	查询待审批的单据
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 * @param taskDefKey 任务key
	 */
	public static String getMyTaskSql(String userName,String flowCode,String taskDefKey) {
		StringBuilder sb = new StringBuilder(" ");
		sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
		sb.append(" RIGHT JOIN (");
		sb.append(" select * from (");
		sb.append(" (select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
		sb.append(" WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' ");
		sb.append("	and ( I.USER_ID_ = '"+userName+"'  or I.GROUP_ID_ IN ( select g.GROUP_ID_ from ACT_ID_MEMBERSHIP g where g.USER_ID_ = '"+userName+"'  )  ) ");
		sb.append(" and RES.SUSPENSION_STATE_ = 1 ");
		sb.append(" ) union (select distinct RES.*  from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = '"+userName+"' ");
		sb.append(" )) v ");
		sb.append(" ) art ");
		sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
		sb.append(" where fd.RELATION_CODE = '"+flowCode+"'  ");
		sb.append(" and art.TASK_DEF_KEY_ = '"+taskDefKey+"' ");
	    return sb.toString();
	}
	
	
	/**
	 * 	查询我的审批的单据
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 * @param taskDefKey 任务key
	 */
	public static String getMyRunningTaskSql(String userName,String flowCode) {
		StringBuilder sb = new StringBuilder(" ");
		sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
		sb.append(" RIGHT JOIN (");
		sb.append(" select * from (");
		sb.append(" (select distinct RES.*  from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = '"+userName+"' ");
		sb.append(" )) v ");
		sb.append(" ) art ");
		sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
		sb.append(" where fd.RELATION_CODE = '"+flowCode+"'  ");
	    return sb.toString();
	}
	
	/**
	 * 	查询待我签收的审批的单据
	 * @param userName 用户账号
	 * @param flowCode 流程唯一编码
	 * @param taskDefKey 任务key
	 */
	public static String getMyClaimTaskSql(String userName,String flowCode) {
		StringBuilder sb = new StringBuilder(" ");
		sb.append(" select fd.FORM_DATA_ID from EXT_ACT_FLOW_DATA fd ");
		sb.append(" RIGHT JOIN (");
		sb.append(" select * from (");
		sb.append(" (select distinct RES.* from ACT_RU_TASK RES inner join ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
		sb.append(" WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' ");
		sb.append("	and ( I.USER_ID_ = '"+userName+"'  or I.GROUP_ID_ IN ( select g.GROUP_ID_ from ACT_ID_MEMBERSHIP g where g.USER_ID_ = '"+userName+"'  )  ) ");
		sb.append(" and RES.SUSPENSION_STATE_ = 1 ");
		sb.append(" )");
		sb.append(" ) v ");
		sb.append(" ) art ");
		sb.append(" on fd.PROCESS_INST_ID = art.PROC_INST_ID_ ");
		sb.append(" where fd.RELATION_CODE = '"+flowCode+"'  ");
	    return sb.toString();
	}
	

}
